package com.kms.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kms.console.JSchConsole;
import com.kms.derby.Connections;
import com.kms.model.MSyspara;
import com.kms.model.SystemParameterBean;
import com.kms.service.SystemparameterService;

public class SystemInfoServlet extends HttpServlet {
	SystemparameterService sysservice ;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException{
		sysservice = (SystemparameterService)Connections.getServiceBean(request, "sysparaService");
		String host="" ;
		String user ="";
		String pwd = "";
		String dir ="";
		int port =0 ;
		String id = request.getParameter("id");
		MSyspara mssys;
		if(request.getParameter("id")!=null ){
			mssys = sysservice.selectmSyspara(Integer.valueOf(id));
			if(mssys!=null){
				host = mssys.getOsip();
				user = mssys.getOsuser();
				pwd = mssys.getOspwd();
				dir = mssys.getDbzipdir();
				port = Integer.valueOf(mssys.getOsport().equals("")?"0":mssys.getOsport());
			}
		}
		
		HttpSession session = request.getSession();
		JSchConsole jc = new JSchConsole();
		StringBuffer command = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String datetime = sdf.format(new Date());
		List<SystemParameterBean> listsysbean = getListSysSetting(request);

		Map<String, SystemParameterBean> map = new HashMap<String, SystemParameterBean>();
		for (SystemParameterBean sysb : listsysbean) {
			map.put(sysb.getName(), sysb);
		}
		String ret = "";
		List<String[]> infocommandlist =new ArrayList<String[]>(); 
		infocommandlist.add(new String[]{"CUP","cat /proc/cpuinfo ;"});
		infocommandlist.add(new String[]{"Memory","free"});
		infocommandlist.add(new String[]{"Harddisk","df -h ;"});
		///
		infocommandlist.add(new String[]{"NeWork","/sbin/ifconfig -a"});
		
		Map infomap = new HashMap();
		try {
			for(String[] infotype:infocommandlist){
				ret = jc.sshExecute(host, user, pwd, infotype[1], port);
				infomap.put(infotype[0], ret+"");
			}
		} catch (Exception e) {
			ret = e.toString();
		}
		
		request.setAttribute("infomap", infomap);
		request.setAttribute("listmgroup",  sysservice.selectAllMGroup());
		request.getRequestDispatcher("serverinfo.jsp").forward(request,response);
		return;
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		sysservice = (SystemparameterService)Connections.getServiceBean(request, "sysparaService");
		doGet(request, response);
	}

	public List<SystemParameterBean> getListSysSetting(
			HttpServletRequest request) {
		List<SystemParameterBean> listsysbean = new ArrayList<SystemParameterBean>();
		try {
			Connection conn = Connections.getConnection(request);
			String SQL = "SELECT * FROM SYSSETTING";
			PreparedStatement stmt = conn.prepareStatement(SQL);
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				SystemParameterBean sysb = new SystemParameterBean();
				sysb.setId(rs.getInt("Id"));
				sysb.setName(rs.getString("Name"));
				sysb.setType(rs.getString("Type"));
				sysb.setValue(rs.getString("Value"));
				sysb.setEnabled(rs.getString("Enabled"));
				sysb.setDescription(rs.getString("Description"));
				listsysbean.add(sysb);
			}

			rs.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return listsysbean;
	}
}
